package com.tps.service.security;

import com.tps.domain.User;
import com.tps.repository.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author charlee.wu
 * @date 2018/5/8 15:30
 **/
@Service
public class SecurityUserDetailService implements UserDetailsService {

    private UserRepository userRepository;

    public SecurityUserDetailService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(s);
        if (ObjectUtils.isEmpty(user)) {
            throw new UsernameNotFoundException(s + " not found.");
        }
        return user;
    }
}
